﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Swift;
using Swift.Lib;

namespace CB.Logic
{
    /// <summary>
    /// 拼图匹配游戏
    /// </summary>
    public class ImageGame : GameData<ImageBlock>
    {
        public override IGameData Init(int w, int h, int c, int srandSeed)
        {
            base.Init(w, h, c, srandSeed);

            for (int i = 0; i < h; i++)
            {
                var l = new List<ImageBlock>();
                for (int j = 0; j < w; j++)
                {
                    var b = new ImageBlock()
                    {
                        X = j,
                        TX = j,
                        Y = i,
                        TY = i
                    };
                    l.Add(b);
                }
                Blocks.Add(l);
            }

            var ex = SRandom.Next(0, w);
            var ey = SRandom.Next(0, h);
            EmptyBlock = Blocks[ey][ex];
            EmptyBlock.IsEmpty = true;

            // 使用移动方式随机打乱所有色块
            while (GameFinish)
            {
                RandomMove(SRandom, 5);
            }

            return this;
        }

        public override bool GameFinish
        {
            get
            {
                for (int i = 0; i < Height; i++)
                {
                    for (int j = 0; j < Width; j++)
                    {
                        if (!BlockMatch(j, i)) return false;
                    }
                }
                return true;
            }
        }

        public override bool BlockMatch(int x, int y)
        {
            var block = Blocks[y][x];
            return block.X == block.TX && block.Y == block.TY;
        }
    }
}
